package com.zhan.ielts.web.controller.word;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.ielts.domain.vo.word.WordSynonymVo;
import com.zhan.ielts.domain.word.Dictionary;
import com.zhan.ielts.service.word.DictionaryService;
import com.zhan.ielts.service.word.IWordSynonymService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * Created by tlfu on 2016/10/8.
 */
@Controller
@RequestMapping("/word/synonym")
public class WordSynonymController extends BaseControllerImpl<WordSynonymVo, WordSynonymVo> {
    @Autowired
    private IWordSynonymService service;
    @Autowired
    private DictionaryService dictionaryService;

    @Override
    protected BaseService<WordSynonymVo> getBaseService() {
        return service;
    }

    @RequestMapping(value = "selectList", method = RequestMethod.GET)
    public ModelAndView selectList(WordSynonymVo query, @PageableDefault Pageable pageable) {
        Page<WordSynonymVo> page = service.queryPageList(query, pageable);
        ModelAndView mav = new ModelAndView("word/synonym/listSynonym", "page", page);
        mav.addObject("query", query);
        return mav;
    }

    @Override
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addView() {
        return new ModelAndView("word/synonym/addSynonym");
    }

    @RequestMapping(value = "/addEntity", method = RequestMethod.POST)
    public ModelAndView addEntity(WordSynonymVo entity) {
        Dictionary query = new Dictionary();
        query.setWord(entity.getWord());
        query = dictionaryService.queryOne(query);
        if (query != null) {
            entity.setDictionaryId(Integer.parseInt(query.getId()));
        } else {
            ModelAndView model = new ModelAndView("/common/error");
            model.addObject("tips", "单词" + entity.getWord() + "在字典表中不存在！");
            return model;
        }
        entity.setCreateTime(new Date());
        entity.setUpdateTime(entity.getCreateTime());
        service.insert(entity);
        return new ModelAndView("redirect:selectList");
    }

    @Override
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView editView(@PathVariable("id") String id) {
        WordSynonymVo obj = getBaseService().queryById(id);
        Dictionary query = new Dictionary();
        query.setId(String.valueOf(obj.getDictionaryId()));
        query = dictionaryService.queryOne(query);
        if (query != null) {
            obj.setWord(query.getWord());
        }
        return new ModelAndView("word/synonym/editSynonym", "entity", obj);
    }

    @RequestMapping(value = "editEntity", method = RequestMethod.POST)
    public ModelAndView editOne(WordSynonymVo entity) {
        Dictionary query = new Dictionary();
        query.setWord(entity.getWord());
        query = dictionaryService.queryOne(query);
        if (query != null) {
            entity.setDictionaryId(Integer.parseInt(query.getId()));
        } else {
            ModelAndView model = new ModelAndView("/common/error");
            model.addObject("tips", "单词" + entity.getWord() + "在字典表中不存在！");
            return model;
        }
        entity.setUpdateTime(new Date());
        service.updateById(entity);
        return new ModelAndView("redirect:selectList");
    }
}
